package com.ygqh.baby.controller.mobile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.model.YgProvinceModel;
import com.ygqh.baby.model.YgUserAddressModel;
import com.ygqh.baby.po.YgUser;
import com.ygqh.baby.service.YgUserAddressService;
import com.ygqh.baby.service.impl.DocumentConstant;
import com.ygqh.baby.utils.IDAuthUtil;
import com.ygqh.baby.utils.SessionUtil;

/**
 * 收货地址
 * 
 * @author guohao
 *
 */
@Controller
@RequestMapping("/mobile/userAddress")
public class YgUserAddressController {
	protected final Logger logger = Logger.getLogger(this.getClass());
	@Autowired
	private YgUserAddressService addressService;
	@Autowired
	private DocumentConstant documentConstant;

	/**
	 * 根据收货地址id获取收货地址详细信息
	 * 
	 * @param callback
	 * @return
	 */
	@RequestMapping(value = "findUserAddressById", method = RequestMethod.GET)
	@ResponseBody
	public JSONPObject findUserAddressById(Long userAddressId, String callback) {
		YgUser user = SessionUtil.getCurrentUser();
		if (userAddressId == -1l) {
			userAddressId = null;
		}
		YgUserAddressModel list = addressService.findUserAddressById(userAddressId, user.getId());
		JSONPObject json = new JSONPObject(callback, Message.success(documentConstant.MESSAGE_SUCCESS, list));
		return json;
	}

	/**
	 * 个人中心获取用户收货地址列表
	 * 
	 * @param callback
	 * @return
	 */
	@RequestMapping(value = "findUserAddressList", method = RequestMethod.GET)
	@ResponseBody
	public JSONPObject findUserAddressList(String callback) {
		YgUser user = SessionUtil.getCurrentUser();
		List<YgUserAddressModel> list = addressService.find(user.getId());
		JSONPObject json = new JSONPObject(callback, Message.success(documentConstant.MESSAGE_SUCCESS, list));
		return json;
	}

	/**
	 * 获取默认收货地址 （没有默认返回最近添加的地址）
	 * 
	 * @param callback
	 * @return
	 */
	@RequestMapping(value = "findDefaultUserAddress", method = RequestMethod.GET)
	@ResponseBody
	public JSONPObject findDefaultUserAddress(Long userAddressId, String callback) {
		YgUser user = SessionUtil.getCurrentUser();
		if (userAddressId == -1l) {
			userAddressId = null;
		}
		YgUserAddressModel userAddress = addressService.findDefaultUserAddress(user.getId(), userAddressId);
		if (userAddress != null && StringUtils.isNotBlank(userAddress.getIdentityNo())) {
			userAddress.setIdentityNo(IDAuthUtil.idNoAddStar(IDAuthUtil.decryptIdNo(userAddress.getIdentityNo())));
		}
		JSONPObject json = new JSONPObject(callback, Message.success(documentConstant.MESSAGE_SUCCESS, userAddress));
		return json;
	}

	/**
	 * 添加收货地址
	 * 
	 * @param receiver
	 *            接收人
	 * @param telPhone
	 *            联系电话
	 * @param provinceName
	 *            省
	 * @param cityName
	 *            市
	 * @param areaName
	 *            区
	 * @param address
	 *            详细地址
	 * @param postCode
	 *            邮政编号
	 * @param isDefault
	 *            是否默认
	 * @param callback
	 * @return
	 */
	@RequestMapping(value = "addUserAddress")
	@ResponseBody
	public JSONPObject addUserAddress(String receiver, String telPhone, String provinceName, String cityName, String areaName, String address, String postCode,
			Boolean isDefault, String callback) {
		YgUser user = SessionUtil.getCurrentUser();
		Message msg = null;

		int row = addressService.addUserAddress(user.getId(), receiver, telPhone, provinceName, cityName, areaName, address, postCode, isDefault);
		if (row > 0) {
			msg = Message.success("操作成功", row);

		} else {
			msg = Message.error("操作失败", row);
		}

		JSONPObject json = new JSONPObject(callback, msg);

		return json;
	}

	/**
	 * 更新收货地址
	 * 
	 * @param addressId
	 *            收货地址id
	 * @param receiver
	 *            收货人
	 * @param telPhone
	 *            联系电话
	 * @param provinceName
	 *            省
	 * @param cityName
	 *            市
	 * @param areaName
	 *            区
	 * @param address
	 *            详细地址
	 * @param postCode
	 *            邮政编码
	 * @param isDefault
	 *            是否默认
	 * @param callback
	 * @return
	 */
	@RequestMapping(value = "updateUserAddress")
	@ResponseBody
	public JSONPObject updateUserAddress(Long addressId, String receiver, String telPhone, String provinceName, String cityName, String areaName,
			String address, String postCode, Boolean isDefault, String callback) {
		YgUser user = SessionUtil.getCurrentUser();
		Message msg = null;
		int row = addressService.updateUserAddress(user.getId(), addressId, receiver, telPhone, provinceName, cityName, areaName, address, postCode, isDefault);
		if (row > 0) {
			msg = Message.success("操作成功", row);

		} else {
			msg = Message.error("操作失败", row);
		}

		JSONPObject json = new JSONPObject(callback, msg);

		return json;
	}

	/**
	 * 将收货地址设置为默认
	 * 
	 * @param addressId
	 * @param isDefault
	 * @param callback
	 * @return
	 */
	@RequestMapping(value = "setDefault")
	@ResponseBody
	public JSONPObject setDefalut(Long addressId, Boolean isDefault, String callback) {
		YgUser user = SessionUtil.getCurrentUser();
		Message msg = null;
		int row = addressService.updateUserAddress(user.getId(), addressId, null, null, null, null, null, null, null, isDefault);
		if (row > 0) {
			msg = Message.success("操作成功", row);

		} else {
			msg = Message.error("操作失败", row);
		}

		JSONPObject json = new JSONPObject(callback, msg);

		return json;
	}

	/**
	 * 删除收货地址
	 * 
	 * @param addressId
	 * @param callback
	 * @return
	 */
	@RequestMapping(value = "deleteUserAddress")
	@ResponseBody
	public JSONPObject deleteUserAddress(Long addressId, String callback) {
		YgUser user = SessionUtil.getCurrentUser();
		Message msg = null;
		int row = addressService.deleteSoft(addressId, user.getId());
		if (row > 0) {
			msg = Message.success("操作成功", row);

		} else {
			msg = Message.error("操作失败", row);
		}

		JSONPObject json = new JSONPObject(callback, msg);

		return json;
	}

	@RequestMapping(value = "findCityList")
	@ResponseBody
	public Object findCityList() {
		List<YgProvinceModel> list = addressService.findCityList();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("varprovinceList", list);
		return map;
	}
}
